# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy.utils.project import get_project_settings

import json

class BbsPipeline(object):
    def process_item(self, item, spider):
        return item

class ForumPipeline(object):
    """管道程序实现将提取的数据依次写入json文件"""
    def __init__(self):
        """构造函数"""
        super().__init__()
        # 读取数据文件路径
        json_path = get_project_settings().get("DATA_JSON")
        # 以二进制写的模式打开数据文件
        self.data_json = open(json_path,mode="wb")

    def process_item(self, item, spider):
        # 先将item转化为字典，再由json转化为字符串
        text = json.dumps(obj=dict(item), ensure_ascii=False)
        # 以utf-8的编码格式写入json文件
        self.data_json.write(text.encode("utf-8"))
        return item